package com.hzhr.iot.cqgas.ds.service;

import com.hzhr.iot.core.constant.CommonOptions;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.cqgas.ds.dao.device.example.DevIotDeviceTypeDao;
import com.hzhr.iot.cqgas.ds.dao.smartmeter.example.EneMeterTypeDao;
import com.hzhr.iot.cqgas.ds.dao.smartmeter.example.SmConcentratorTypeDao;
import com.hzhr.iot.cqgas.ds.dao.smartmeter.example.SmDtuTypeDao;
import com.hzhr.iot.cqgas.ds.dao.user.example.UsrCompanyDao;
import com.hzhr.iot.cqgas.ds.entity.device.DevIotDeviceTypeEntity;
import com.hzhr.iot.cqgas.ds.entity.device.DevIotDeviceTypeExample;
import com.hzhr.iot.cqgas.ds.entity.smartmeter.*;
import com.hzhr.iot.cqgas.ds.entity.user.UsrCompanyEntity;
import com.hzhr.iot.cqgas.ds.entity.user.UsrCompanyExample;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class IotDeviceTypeService {
    @Autowired
    DevIotDeviceTypeDao devIotDeviceTypeDao;
    @Autowired
    EneMeterTypeDao eneMeterTypeDao;
    @Autowired
    DevOptionsService devOptionsService;
    @Autowired
    SmDtuTypeDao smDtuTypeDao;
    @Autowired
    SmConcentratorTypeDao smConcentratorTypeDao;
    @Autowired
    UsrCompanyDao usrCompanyDao;

    private Logger logger = LoggerFactory.getLogger(IotDeviceTypeService.class);

    public void syncIotDeviceType() {
        int addCount = 0;
        int updateCount = 0;
        long start = new Date().getTime();

        //同步gprs物联网表型号
        String iotCategoryCode = devOptionsService.getCode("IOT_DEVICE_CATEGORY", "GPRS_IOT_GAS_METER");
        EneMeterTypeExample eneMeterTypeExample = new EneMeterTypeExample();
        eneMeterTypeExample.createCriteria()
                .andRemoteMeterIdEqualTo("130");
        List<EneMeterTypeEntity> eneMeterTypeEntities = eneMeterTypeDao.selectByExample(eneMeterTypeExample);

        for (EneMeterTypeEntity eneMeterTypeEntity :
        eneMeterTypeEntities) {
            try {
                String mtId = eneMeterTypeEntity.getMtId();

                DevIotDeviceTypeExample devIotDeviceTypeExample = new DevIotDeviceTypeExample();
                devIotDeviceTypeExample.createCriteria()
                        .andOutIdEqualTo(mtId)
                        .andDeviceCategoryCodeEqualTo(iotCategoryCode);
                List<DevIotDeviceTypeEntity> devIotDeviceTypeEntities1 = devIotDeviceTypeDao.selectByExample(devIotDeviceTypeExample);
                if (devIotDeviceTypeEntities1.size() > 0) {
                    DevIotDeviceTypeEntity devIotDeviceTypeEntity = devIotDeviceTypeEntities1.get(0);
                    devIotDeviceTypeEntity.setDes(HzhrTypeUtil.object2string(eneMeterTypeEntity.getMtModel()));

                    UsrCompanyExample usrCompanyExample = new UsrCompanyExample();
                    usrCompanyExample.createCriteria().andOutIdEqualTo(eneMeterTypeEntity.getPrId());
                    List<UsrCompanyEntity> usrCompanyEntities = usrCompanyDao.selectByExample(usrCompanyExample);
                    UsrCompanyEntity usrCompanyEntity = usrCompanyEntities.get(0);
                    devIotDeviceTypeEntity.setCompanyId(usrCompanyEntity.getId());

                    devIotDeviceTypeEntity.setUpdateTime(new Date());
                    devIotDeviceTypeEntity.setUpdateUserId(0);
                    devIotDeviceTypeEntity.setIsValid(CommonOptions.YES_NO.YES);
                    devIotDeviceTypeEntity.setState("1");
                    devIotDeviceTypeDao.updateByPrimaryKey(devIotDeviceTypeEntity);
                    updateCount++;
                } else {
                    DevIotDeviceTypeEntity devIotDeviceTypeEntity = new DevIotDeviceTypeEntity();
                    devIotDeviceTypeEntity.setOutId(eneMeterTypeEntity.getMtId());
                    devIotDeviceTypeEntity.setDes(HzhrTypeUtil.object2string(eneMeterTypeEntity.getMtModel()));

                    UsrCompanyExample usrCompanyExample = new UsrCompanyExample();
                    usrCompanyExample.createCriteria().andOutIdEqualTo(eneMeterTypeEntity.getPrId());
                    List<UsrCompanyEntity> usrCompanyEntities = usrCompanyDao.selectByExample(usrCompanyExample);
                    if (usrCompanyEntities.size() == 0) devIotDeviceTypeEntity.setCompanyId(0);
                    else {
                        UsrCompanyEntity usrCompanyEntity = usrCompanyEntities.get(0);
                        devIotDeviceTypeEntity.setCompanyId(usrCompanyEntity.getId());
                    }

                    devIotDeviceTypeEntity.setCreateTime(new Date());
                    devIotDeviceTypeEntity.setCreateUserId(0);
                    devIotDeviceTypeEntity.setIsValid(CommonOptions.YES_NO.YES);
                    devIotDeviceTypeEntity.setState("1");
                    devIotDeviceTypeEntity.setDeviceCategoryCode(iotCategoryCode);
                    devIotDeviceTypeDao.insert(devIotDeviceTypeEntity);
                    addCount++;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        //同步dtu型号
        String dtuCategoryCode = devOptionsService.getCode("IOT_DEVICE_CATEGORY", "DTU");
        SmDtuTypeExample smDtuTypeExample = new SmDtuTypeExample();
        List<SmDtuTypeEntity> smDtuTypeEntities = smDtuTypeDao.selectByExample(smDtuTypeExample);

        for (SmDtuTypeEntity smDtuTypeEntity :
                smDtuTypeEntities) {
            try {
                String dtuId = smDtuTypeEntity.getDtutId();

                DevIotDeviceTypeExample devIotDeviceTypeExample = new DevIotDeviceTypeExample();
                devIotDeviceTypeExample.createCriteria()
                        .andOutIdEqualTo(dtuId)
                        .andDeviceCategoryCodeEqualTo(dtuCategoryCode);
                List<DevIotDeviceTypeEntity> devIotDeviceTypeEntities1 = devIotDeviceTypeDao.selectByExample(devIotDeviceTypeExample);
                if (devIotDeviceTypeEntities1.size() > 0) {
                    DevIotDeviceTypeEntity devIotDeviceTypeEntity = devIotDeviceTypeEntities1.get(0);
                    devIotDeviceTypeEntity.setDes(HzhrTypeUtil.object2string(smDtuTypeEntity.getDtutModel()));

                    UsrCompanyExample usrCompanyExample = new UsrCompanyExample();
                    usrCompanyExample.createCriteria().andOutIdEqualTo(smDtuTypeEntity.getPrId());
                    List<UsrCompanyEntity> usrCompanyEntities = usrCompanyDao.selectByExample(usrCompanyExample);
                    if (usrCompanyEntities.size() == 0) devIotDeviceTypeEntity.setCompanyId(0);
                    else {
                        UsrCompanyEntity usrCompanyEntity = usrCompanyEntities.get(0);
                        devIotDeviceTypeEntity.setCompanyId(usrCompanyEntity.getId());
                    }

                    devIotDeviceTypeEntity.setUpdateTime(new Date());
                    devIotDeviceTypeEntity.setUpdateUserId(0);
                    devIotDeviceTypeEntity.setIsValid(CommonOptions.YES_NO.YES);
                    devIotDeviceTypeEntity.setState("1");
                    devIotDeviceTypeDao.updateByPrimaryKey(devIotDeviceTypeEntity);
                    updateCount++;
                } else {
                    DevIotDeviceTypeEntity devIotDeviceTypeEntity = new DevIotDeviceTypeEntity();
                    devIotDeviceTypeEntity.setOutId(smDtuTypeEntity.getDtutId());
                    devIotDeviceTypeEntity.setDes(HzhrTypeUtil.object2string(smDtuTypeEntity.getDtutModel()));

                    UsrCompanyExample usrCompanyExample = new UsrCompanyExample();
                    usrCompanyExample.createCriteria().andOutIdEqualTo(smDtuTypeEntity.getPrId());
                    List<UsrCompanyEntity> usrCompanyEntities = usrCompanyDao.selectByExample(usrCompanyExample);
                    UsrCompanyEntity usrCompanyEntity = usrCompanyEntities.get(0);
                    devIotDeviceTypeEntity.setCompanyId(usrCompanyEntity.getId());

                    devIotDeviceTypeEntity.setCreateTime(new Date());
                    devIotDeviceTypeEntity.setCreateUserId(0);
                    devIotDeviceTypeEntity.setIsValid(CommonOptions.YES_NO.YES);
                    devIotDeviceTypeEntity.setState("1");
                    devIotDeviceTypeEntity.setDeviceCategoryCode(dtuCategoryCode);
                    devIotDeviceTypeDao.insert(devIotDeviceTypeEntity);
                    addCount++;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        //同步集中器型号
        String ccrCategoryCode = devOptionsService.getCode("IOT_DEVICE_CATEGORY", "CONCENTRATOR");
        SmConcentratorTypeExample smConcentratorTypeExample = new SmConcentratorTypeExample();
        List<SmConcentratorTypeEntity> smConcentratorTypeEntities = smConcentratorTypeDao.selectByExample(smConcentratorTypeExample);

        for (SmConcentratorTypeEntity smConcentratorTypeEntity :
                smConcentratorTypeEntities) {
            try {
                String cctId = smConcentratorTypeEntity.getCctId();

                DevIotDeviceTypeExample devIotDeviceTypeExample = new DevIotDeviceTypeExample();
                devIotDeviceTypeExample.createCriteria()
                        .andOutIdEqualTo(cctId)
                        .andDeviceCategoryCodeEqualTo(ccrCategoryCode);
                List<DevIotDeviceTypeEntity> devIotDeviceTypeEntities1 = devIotDeviceTypeDao.selectByExample(devIotDeviceTypeExample);
                if (devIotDeviceTypeEntities1.size() > 0) {
                    DevIotDeviceTypeEntity devIotDeviceTypeEntity = devIotDeviceTypeEntities1.get(0);
                    devIotDeviceTypeEntity.setDes(HzhrTypeUtil.object2string(smConcentratorTypeEntity.getCctModel()));

                    UsrCompanyExample usrCompanyExample = new UsrCompanyExample();
                    usrCompanyExample.createCriteria().andOutIdEqualTo(smConcentratorTypeEntity.getPrId());
                    List<UsrCompanyEntity> usrCompanyEntities = usrCompanyDao.selectByExample(usrCompanyExample);
                    if (usrCompanyEntities.size() == 0) devIotDeviceTypeEntity.setCompanyId(0);
                    else {
                        UsrCompanyEntity usrCompanyEntity = usrCompanyEntities.get(0);
                        devIotDeviceTypeEntity.setCompanyId(usrCompanyEntity.getId());
                    }

                    devIotDeviceTypeEntity.setUpdateTime(new Date());
                    devIotDeviceTypeEntity.setUpdateUserId(0);
                    devIotDeviceTypeEntity.setIsValid(CommonOptions.YES_NO.YES);
                    devIotDeviceTypeEntity.setState("1");
                    devIotDeviceTypeDao.updateByPrimaryKey(devIotDeviceTypeEntity);
                    updateCount++;
                } else {
                    DevIotDeviceTypeEntity devIotDeviceTypeEntity = new DevIotDeviceTypeEntity();
                    devIotDeviceTypeEntity.setOutId(smConcentratorTypeEntity.getCctId());
                    devIotDeviceTypeEntity.setDes(HzhrTypeUtil.object2string(smConcentratorTypeEntity.getCctModel()));

                    UsrCompanyExample usrCompanyExample = new UsrCompanyExample();
                    usrCompanyExample.createCriteria().andOutIdEqualTo(smConcentratorTypeEntity.getPrId());
                    List<UsrCompanyEntity> usrCompanyEntities = usrCompanyDao.selectByExample(usrCompanyExample);
                    UsrCompanyEntity usrCompanyEntity = usrCompanyEntities.get(0);
                    devIotDeviceTypeEntity.setCompanyId(usrCompanyEntity.getId());

                    devIotDeviceTypeEntity.setCreateTime(new Date());
                    devIotDeviceTypeEntity.setCreateUserId(0);
                    devIotDeviceTypeEntity.setIsValid(CommonOptions.YES_NO.YES);
                    devIotDeviceTypeEntity.setState("1");
                    devIotDeviceTypeEntity.setDeviceCategoryCode(ccrCategoryCode);
                    devIotDeviceTypeDao.insert(devIotDeviceTypeEntity);
                    addCount++;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        long end = new Date().getTime();
        logger.info("同步物联网设备型号结束: " + (end - start) / 1000 + "s");
        logger.info("同步时间: " + (end - start) / 1000 + "s");
        logger.info("更新数量: " + updateCount + "条");
        logger.info("新增数量: " + addCount + "条");

    }

}
